const bcrypt = require('bcryptjs');
const mysql = require('mysql2/promise');
const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '../../.env') });

async function createAdminUser() {
    const password = 'admin123';
    const hashedPassword = await bcrypt.hash(password, 10);
    
    const connection = await mysql.createConnection({
        host: process.env.DB_HOST,
        user: process.env.DB_USER,
        password: process.env.DB_PASSWORD,
        database: process.env.DB_NAME
    });

    try {
        await connection.execute(`
            CREATE TABLE IF NOT EXISTS admin_users (
                id INT AUTO_INCREMENT PRIMARY KEY,
                username VARCHAR(50) UNIQUE NOT NULL,
                password VARCHAR(255) NOT NULL,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
            )
        `);

        await connection.execute(
            'INSERT INTO admin_users (username, password) VALUES (?, ?)',
            ['admin', hashedPassword]
        );

        console.log('管理员账户创建成功！');
        console.log('用户名: admin');
        console.log('密码: admin123');
    } catch (error) {
        if (error.code === 'ER_DUP_ENTRY') {
            console.log('管理员账户已存在');
        } else {
            console.error('创建理员账户时出错:', error);
        }
    } finally {
        await connection.end();
    }
}

createAdminUser(); 